//承运商附件管理

interface ICarrierAttachmentEditViewModel extends ng.IScope {
    /**
     * 头像上传组件
     */
    uploader: any;
    /**
     * 身份证上传组件
     */
    IDcard: any;
    /**
     * 营业执照上传组件
     */
    businessUploader: any;
    /**
     * 税务登记执证
     */
    taxUploader: any;
    /**
     * 道路经营许可证
     */
    wayUploader: any;
    /**
     * 保存附件
     */
    save: () => void;

     /**
     * 点击图片时候触发，用以旋转放大图片
     */
    handleImgClick:(item:any)=>void;
    /**
     * 用以旋转放大的图片的路径
     */
    selectedImgSrc:string;

    attachList:OssUploaderItem[];
}

class CarrierAttachmentEditController {
    uploaderList: AddCarrierAttachmentDetail[] = [];
    baseImageUrl: string;
    carrierId: string;

    constructor(private $scope: ICarrierAttachmentEditViewModel, private routeService: routeService,  private carrierService: ICarrierService,
        private $location: ng.ILocationService, private $http: ng.IHttpService, private $ngBootbox: BootboxService,private commonService:ICommonService,private ossUploaderService:ossUploaderService) {
        this.baseImageUrl = this.routeService.getBaseUrl() + "attachment/";
        //this.uploaderList = [];
        this.init();
    }

    init(): void {
        let loginData = JSON.parse(window.localStorage.getItem("loginData"));
        this.carrierId = this.$location.search().carrierId;
        this.carrierId = JSON.parse(window.localStorage.getItem("carrier").toString()).carrierId;
        
        
        this.$scope.save = this.save;
        
       
        const OssFile = this.ossUploaderService.getOssFile();

        this.$scope.attachList=[
            
            {
                title:'头像',
                uploader:null,
                uploaderName:'headPhotoUploader',

            },
            {
                title:'身份证',
                uploader:null,
                uploaderName:'IDcardUploader',

            },
            {
                title:'营业执照',
                uploader:null,
                uploaderName:'businessUploader',

            },
            {
                title:'税务登记证',
                uploader:null,
                uploaderName:'taxUploader',

            },
            {
                title:'道路经营许可证',
                uploader:null,
                uploaderName:'wayUploader',

            }
        ]
        this.$scope.attachList.forEach((item,index)=>{
            item.uploader=new OssFile(this.$scope,item.uploader,{uploaderList:this.uploaderList,tag:index+1});
            item.uploader.onSuccess=(it)=>{
                console.log('onsuccess',it);
                this.onSuccessItem(it.fileName,it.imgSrc,index+1)
    
            }
            

        })

        this.loadImg();

        this.$scope.handleImgClick=(item)=>{
            // console.log(item)
            this.$scope.selectedImgSrc=item.imgSrc;
        }
    }

    loadImg=()=>{
        this.carrierService.getDetail(this.carrierId).then((res) => {
            if (res.data.attachmentList.length <= 0) {
                return;
            }
            res.data.attachmentList.forEach((item, index) => {
                this.uploaderList.push(item);
                
                this.$scope.attachList[Number(item.attachmentTag)-1].uploader.addByPath(item.attachmentPath,{id:item.attachmentId,name:item.attachmentName})


            });
        });
    }

   
    /**
     * 选择上传文件后
     */
    onSuccessItem = (fileName: string, filePath: string, tag: number) => {
        let attachment = {
            attachmentId: "",
            attachmentName: fileName,
            attachmentPath: filePath,
            attachmentTag: tag.toString()
        };
        this.uploaderList.push(attachment);
    }

    /**
     * 删除上传的附件
     */
    onDelete = (item: any,num:number) => {
        $("input[type=file]").val("");
        let imgName = item._file.name;
        var types = item.types || num ;
        this.uploaderList.forEach((item, index) => {
            if (item.attachmentName == imgName) {
                this.uploaderList.splice(index, 1);
            }
        });
        switch(types){
            case 1: this.$scope.uploader.isLoad = true;
                break;
            case 2: this.$scope.IDcard.isLoad = true;
                break;
            case 3: this.$scope.businessUploader.isLoad = true;
                break;
            case 4: this.$scope.taxUploader.isLoad = true;
                break;
            case 5: this.$scope.wayUploader.isLoad = true;
                break;
        }
    }

    /**
     * 下载附件
     */
    // onDownload = (item: any) => {
    //     let imgName = item._file.name;
    //     this.uploaderList.forEach((item, index) => {
    //         if (item.attachmentName == imgName) {
    //             window.location.href = this.baseImageUrl + item.attachmentId;
    //         }
    //     });
    // }

    /**
     * 保存
     */
    save = () => {
        this.carrierService.addAttachment(this.carrierId, this.uploaderList).then((result) => {
            // window.location.reload();
        });
    }
}

angular.module("tms").controller("carrierAttachmentEditController", ["$scope", "routeService", "carrierService", "$location", "$http", "$ngBootbox","commonService","ossUploaderService", CarrierAttachmentEditController]);
